package gxdc;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class DataPull {


    private static String appKey = "dcba00422eff453cb6d0739fd87b7302";
    private static int pageSize = 2;
    //    private static int totalCount = 244638540;
    private static int totalCount = 100;

    // 一共有多少页
    private static int totalPage = totalCount / pageSize + 1;


    private static String postionPath = "D:\\gxdc\\postion.txt";
    private static String dataPath = "D:\\gxdc\\data.txt";

    public static void main(String[] args) throws Exception {
        BufferedWriter postionBr = new BufferedWriter(new FileWriter(postionPath));
        BufferedWriter dataBr = new BufferedWriter(new FileWriter(dataPath));

        for (int pageNum = 1; pageNum <= totalPage; pageNum++) {
            String url = "https://opendata.sz.gov.cn/api/29200_00403627/1/service.xhtml?appKey="
                    + appKey + "&page=" + pageNum + "&rows=" + pageSize;
            String cookieString = "";
            String result = sendRequestWithCookie(url, cookieString);
            // 解析result，并记录数据
            dataBr.write(result);
            dataBr.newLine();

            // 记录当前写到了哪一页
            postionBr.write("拉取到了第" + pageNum + "页数据");
            postionBr.newLine();

            // 刷新缓冲区
            dataBr.flush();
            postionBr.flush();

            System.out.println("处理第" + pageNum + "页数据成功，数据：" + result);
        }


    }

    public static String sendRequestWithCookie(String urlStr, String cookieString) throws IOException {
        URL url = new URL(urlStr);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("GET");
        connection.setRequestProperty("Cookie", cookieString);

        int responseCode = connection.getResponseCode();

        StringBuilder response = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
        }

        connection.disconnect();

        return response.toString();
    }
}
